package top.hkyzf.study.leetcode.graph;

import org.junit.Test;

import java.util.*;

/**
 * @author 朱峰
 * @date 2022-1-27 13:13
 */
public class 腐烂的橘子_994 {
    public int orangesRotting(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        Queue<int[]> queue = new LinkedList<>();
        int temp = 0;
        boolean[][] watch = new boolean[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int cur = grid[i][j];
                if (cur == 0) {
                    watch[i][j] = true;
                } else if (cur == 1) {
                    temp++;
                } else if (cur == 2) {
                    queue.offer(new int[]{i, j});
                    watch[i][j] = true;
                }
            }
        }
        if (temp == 0) {
            return 0;
        }
        int result = bfs(grid, watch, queue);
        temp = 0;
        for (boolean[] b : watch) {
            for (int j = 0; j < n; j++) {
                if (!b[j]) {
                    temp++;
                }
            }
        }
        return temp != 0 ? -1 : result;
    }

    private int bfs(int[][] grid, boolean[][] watch, Queue<int[]> queue) {
        int[] dx = {-1, 1, 0, 0};
        int[] dy = {0, 0, -1, 1};
        int result = 0;
        while (!queue.isEmpty()) {
            int[] ints = queue.poll();
            for (int i = 0; i < 4; i++) {
                int x = ints[0] + dx[i];
                int y = ints[1] + dy[i];
                if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length && !watch[x][y]) {
                    watch[x][y] = true;
                    grid[x][y] = grid[ints[0]][ints[1]] + 1;
                    queue.offer(new int[]{x, y});
                    result = Math.max(result, grid[x][y]);
                }
            }
        }
        return result-2;
    }

    int[] dr = new int[]{-1, 0, 1, 0};
    int[] dc = new int[]{0, -1, 0, 1};

    public int orangesRotting1(int[][] grid) {
        int R = grid.length, C = grid[0].length;
        Queue<Integer> queue = new ArrayDeque<>();
        Map<Integer, Integer> depth = new HashMap<>();
        for (int r = 0; r < R; ++r) {
            for (int c = 0; c < C; ++c) {
                if (grid[r][c] == 2) {
                    int code = r * C + c;
                    queue.add(code);
                    depth.put(code, 0);
                }
            }
        }
        int ans = 0;
        while (!queue.isEmpty()) {
            int code = queue.remove();
            int r = code / C, c = code % C;
            for (int k = 0; k < 4; ++k) {
                int nr = r + dr[k];
                int nc = c + dc[k];
                if (0 <= nr && nr < R && 0 <= nc && nc < C && grid[nr][nc] == 1) {
                    grid[nr][nc] = 2;
                    int ncode = nr * C + nc;
                    queue.add(ncode);
                    depth.put(ncode, depth.get(code) + 1);
                    ans = depth.get(ncode);
                }
            }
        }
        for (int[] row: grid) {
            for (int v: row) {
                if (v == 1) {
                    return -1;
                }
            }
        }
        return ans;
    }

    @Test
    public void testOrangesRotting () {
        int[][] grid = {{2,1,1},{0,1,1},{1,0,1}};
        System.out.println(orangesRotting1(grid));
    }
}
